import axios from 'axios'
import Vue from 'vue'
import { Loading} from 'element-ui';
import { Notification } from 'element-ui';
import $ from 'jquery'
/**
 * ajax调用
 *
 * @param url
 *            链接地址
 * @param data
 *            数据
 * @param callbackfun
 *            回调函数
 * @param method
 *            调用方法，默认为post
 * @param dataType
 *            返回参数类型,默认为json
 */
 export default {

	commitData(url, data, callbackfun, async,dataType ) {
		var that = this;
		var method = "post";
		var contentType = "application/x-www-form-urlencoded;charset=UTF-8";
		if(async == undefined){
			async = true;
		}
		if(dataType == undefined || dataType =="json" ){
			dataType = "json";
			contentType = "application/json;charset=utf-8";
		}
		if (callbackfun.error == null) {
			// 默认输出错误日志
			callbackfun.error = function(data) {
				$.alert("操作异常");
			};
		}else{
			var fun = callbackfun.error;
			callbackfun.error = function(data) {
				fun(data);
			}
		}
		$.ajax({
			url : url,
			dataType : dataType,
			type : method,
			data : data,
			async: async,
			contentType:contentType,
			headers : {
				"returntype" : "ajax/json",
        'token':sessionStorage.getItem('loginAccount')?sessionStorage.getItem('loginAccount'):undefined,
			},
			traditional : true,
			success : function(data) {
				if(data.code === 0){
					callbackfun.success(data);
				} else if(data.code===10000){
          let isFrom = localStorage.getItem('isFrom')
          let CasAddress = localStorage.getItem('CasAddress')
          if(isFrom && isFrom == 'selfLogin'){
            that.$router.push('/')
          }else{
            if(CasAddress && CasAddress!=''){
              window.location.href = CasAddress;
            }else{
              this.$router.push('/')
            }
          }
				} else {
					Notification({
						title: '警告',
						message: data.description,
						type: 'warning'
					})
				}
			},
			error : callbackfun.error
		});
	},

}
